//
//  CircleProgressView.m
//  Civacrm
//
//  Created by zhk on 2017/2/9.
//  Copyright © 2017年 王桂敏. All rights reserved.
//

#import "CircleProgressView.h"

@interface CircleProgressView ()

@property (nonatomic , strong) CAShapeLayer * progressLayer;

@end

@implementation CircleProgressView

- (void)drawRect:(CGRect)rect  // 次方法是view绘图的方法 可以重写 但不要手动调用 如果想要调用 在view类里 （也就是self）调用一个什么setneeds方法 一般不调用只执行一次
{
 
    CGPoint circlePoint = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds)); // 圆点
    CGFloat circleRadius = self.bounds.size.width / 2 ; // 半径
    
    UIBezierPath * circlePath = [UIBezierPath bezierPathWithArcCenter:circlePoint
                                                               radius:circleRadius
                                                           startAngle:kRadiansToDegrees(-90)
                                                             endAngle:kRadiansToDegrees(270)
                                                            clockwise:YES];
    
    CGFloat percentValue = self.currentValue / self.totalValue;
    
    CAShapeLayer * trackLayer = [CAShapeLayer layer]; // 这是一个轨道 一个完整的圆 动画要在这个上面跑
    trackLayer.path = circlePath.CGPath;
    trackLayer.fillColor = [UIColor clearColor].CGColor; // 轨道里面的填充颜色
    trackLayer.strokeColor = ColorWithHexString(@"def2fa").CGColor; // 轨道上面的颜色
    trackLayer.lineCap = kCALineCapRound; // 设置线条拐点样式
    trackLayer.lineWidth = 16.0f;
    [self.layer addSublayer:trackLayer];
    
    self.progressLayer = [CAShapeLayer layer];
    self.progressLayer.path = circlePath.CGPath;
    self.progressLayer.fillColor = [UIColor clearColor].CGColor;
    self.progressLayer.lineWidth = 16.0f;
    self.progressLayer.lineCap = kCALineCapRound;
    self.progressLayer.strokeColor = ColorWithHexString(@"41c3ff").CGColor;
    self.progressLayer.strokeStart = 0;
    self.progressLayer.strokeEnd = percentValue;
    [self.layer addSublayer:self.progressLayer];
    
    
    // 动画
    CABasicAnimation * layerAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    layerAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    layerAnimation.toValue = [NSNumber numberWithFloat:percentValue];
    layerAnimation.duration = 1.0f;
    layerAnimation.removedOnCompletion = YES;
    [self.progressLayer addAnimation:layerAnimation forKey:@"progress"];
    
    
}

-(void)dealloc
{
    [self.progressLayer removeAnimationForKey:@"justPath"];
}



@end
